﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;

namespace CommonLibrary
{
    public static class ClType
    {
        #region 类型判断

        /// <summary>
        /// 验证是否为布尔类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ClIsBoolean(this object obj)
        {
            string str = obj.ClToString();
            if (str.ClIsNullOrWhiteSpace())
            {
                return false;
            }

            bool outValue;
            str = str == "1" ? "true" : str;
            str = str == "0" ? "false" : str;
            if (bool.TryParse(str, out outValue))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 验证是否为整数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ClIsInt(this object obj)
        {
            string str = obj.ClToString();
            if (str.ClIsNullOrWhiteSpace())
            {
                return false;
            }

            int outValue;
            if (int.TryParse(str, out outValue))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 验证是否为正整数
        /// </summary>
        public static bool ClIsInt(this object str, bool Plus)
        {
            if (str.ClIsInt() == false)
            {
                return false;
            }
            if (Plus)
            {
                return str.ClToInt32() >= 0 ? true : false;
            }

            return str.ClToInt32() >= 0 ? false : true;
        }

        /// <summary>
        /// 判断对象是否为Double类型的数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ClIsDouble(this object obj)
        {
            string str = obj.ClToString();
            if (str.ClIsNullOrWhiteSpace())
            {
                return false;
            }

            double outValue;
            if (double.TryParse(str, out outValue))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 判断对象是否为正Double类型的数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ClIsDouble(this object str, bool Plus)
        {
            if (str.ClIsDouble() == false)
            {
                return false;
            }

            if (Plus)
            {
                return str.ClToDouble() >= 0 ? true : false;
            }

            return str.ClToDouble() >= 0 ? false : true;
        }

        /// <summary>
        /// 验是否可以转换为日期和时间
        /// </summary>
        /// <param name="strDateTime"></param>
        /// <returns></returns>
        public static bool ClIsDateTime(this object obj)
        {
            string str = obj.ClToString();
            if (str.ClIsNullOrWhiteSpace())
            {
                return false;
            }

            DateTime outValue;
            if (DateTime.TryParse(str, out outValue))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 验证是否可以转换为Guid类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ClIsGuid(this object obj)
        {
            string str = obj.ClToString();
            if (str.ClIsNullOrWhiteSpace())
            {
                return false;
            }
            if (str.Length != 36)
            {
                return false;
            }

            try
            {
                Guid gd = new Guid(str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion


        #region 类型转换

        /// <summary>
        /// 转为字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ClToString(this object obj)
        {
            return Convert.ToString(obj);
        }

        /// <summary>
        /// 转为布尔型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ClToBoolean(this object obj)
        {
            return Convert.ToBoolean(obj);
        }

        /// <summary>
        /// 转为整型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ClToInt32(this object obj)
        {
            return Convert.ToInt32(obj);
        }

        /// <summary>
        /// 转为浮点型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static double ClToDouble(this object obj)
        {
            return Convert.ToDouble(obj);
        }

        /// <summary>
        /// 转为日期时间型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime ClToDateTime(this object obj)
        {
            return Convert.ToDateTime(obj);
        }

        /// <summary>
        /// 注意:两个double加减乘除
        /// </summary>
        /// <param name="in_double"></param>
        /// <param name="in_digits"></param>
        /// <returns></returns>
        public static double ClToDouble(this double in_double, int? in_digits)
        {
            //double n = 171.6;
            //double m = 28.17;
            //double k = n + m;
            //此时k会变成199.76999999999998

            return Math.Round(in_double, in_digits ?? 2);
        }

        public static double ClToDouble(this double in_double)
        {
            return in_double.ClToDouble(null);
        }

        /// <summary>
        /// 将分转转为敕数，如199.78->19978
        /// </summary>
        /// <param name="in_amt"></param>
        /// <returns></returns>
        public static string ClToPeny(this double in_amt)
        {
            return Convert.ToString(Convert.ToInt32(in_amt * 100));
        }

        public static string ClToPeny(this decimal in_amt)
        {
            double amt = Convert.ToDouble(in_amt);
            return amt.ClToPeny();
        }

        public static string ClToPeny(this string in_amt)
        {
            double amt = Convert.ToDouble(in_amt);
            return amt.ClToPeny();
        }



        #region ClToMoney 数字转换为含千位符字符串

        /// <summary>
        /// 数字转换为含千位符字符串
        /// </summary>
        /// <param name="val"></param>
        /// <param name="num">小数位</param>
        /// <returns></returns>
        public static string ClToMoney(this double val, int num)
        {
            string value = Convert.ToString(val);
            double dc = 0;
            double.TryParse(value, out dc);

            return dc.ToString("N" + num.ToString());
        }

        public static string ClToMoney(this double val)
        {
            return val.ClToMoney(2);
        }

        public static string ClToMoneySelf(this double val)
        {
            string value = Convert.ToString(val);

            int num = 0;
            if (value.IndexOf(".") != -1)
            {
                num = value.Length - value.LastIndexOf(".") - 1;
            }
            return val.ClToMoney(num);
        }


        public static string ClToMoney(this decimal val, int num)
        {
            return Convert.ToDouble(val).ClToMoney(num);
        }

        public static string ClToMoney(this decimal val)
        {
            return val.ClToMoney(2);
        }

        public static string ClToMoneySelf(this decimal val)
        {
            string value = Convert.ToString(val);

            int dig = 0;
            if (value.IndexOf(".") != -1)
            {
                dig = value.Length - value.LastIndexOf(".") - 1;
            }
            return val.ClToMoney(dig);
        }


        public static string ClToMoney(this string val, int num)
        {
            return Convert.ToDouble(val).ClToMoney(num);
        }

        public static string ClToMoney(this string val)
        {
            return val.ClToMoney(2);
        }

        public static string ClToMoneySelf(this string val)
        {
            string value = Convert.ToString(val);

            int dig = 0;
            if (value.IndexOf(".") != -1)
            {
                dig = value.Length - value.LastIndexOf(".") - 1;
            }
            return val.ClToMoney(dig);
        }


        /// <summary>
        /// 获取千位符的货币格式
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetMoneyFormat(object number)
        {
            int intNum = Convert.ToInt32(number);
            System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
            if (number.ToString().IndexOf(".") > -1)
            {
                nfi.NumberDecimalDigits = 2;
            }
            else
            {
                nfi.NumberDecimalDigits = 0;
            }
            return intNum.ToString("N", nfi);
        }


        public static int GetNumberFromMoneyFormat(object money)
        {
            return Convert.ToInt32(money.ToString().Replace(",", ""));
        }

        #endregion

        #endregion


        #region DateTime

        /// <summary>
        /// DataTime转换所需要的Format
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <param name="in_format"></param>
        /// <returns></returns>
        public static string ClToStringFormat(this DateTime in_datetime, string in_format)
        {
            return Convert.ToDateTime(in_datetime).ToString(in_format);
        }
        /// <summary>
        /// DataTime转换所需要的Format
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <param name="in_format"></param>
        /// <returns></returns>
        public static string ClToStringFormat(this DateTime? in_datetime, string in_format)
        {
            if (in_datetime == null)
            {
                return null;
            }
            return Convert.ToDateTime(in_datetime).ToString(in_format);
        }

        /// <summary>
        /// DataTime转换为yyyy-MM-dd HH:mm:ss.fff
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringFullTime(this DateTime in_datetime)
        {
            return in_datetime.ClToStringFormat("yyyy-MM-dd HH:mm:ss.fff");
        }

        /// <summary>
        /// DataTime转换为yyyy-MM-dd HH:mm:ss.fff
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringFullTime(this DateTime? in_datetime)
        {
            return in_datetime.ClToStringFormat("yyyy-MM-dd HH:mm:ss.fff");
        }

        /// <summary>
        /// DataTime转换为yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringDateTime(this DateTime in_datetime)
        {
            return in_datetime.ClToStringFormat("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// DataTime转换为yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringDateTime(this DateTime? in_datetime)
        {
            return in_datetime.ClToStringFormat("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// DataTime转换为yyyy-MM-dd
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringDate(this DateTime in_datetime)
        {
            return in_datetime.ClToStringFormat("yyyy-MM-dd");
        }

        /// <summary>
        /// DataTime转换为yyyy-MM-dd
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringDate(this DateTime? in_datetime)
        {
            return in_datetime.ClToStringFormat("yyyy-MM-dd");
        }

        /// <summary>
        /// DataTime转换为HH:mm:ss
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringTime(this DateTime in_datetime)
        {
            return in_datetime.ClToStringFormat("HH:mm:ss");
        }

        /// <summary>
        /// DataTime转换为HH:mm:ss
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringTime(this DateTime? in_datetime)
        {
            return in_datetime.ClToStringFormat("HH:mm:ss");
        }

        /// <summary>
        /// DataTime转换为yyyyMMddHHmmss
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringNum(this DateTime in_datetime)
        {
            return in_datetime.ClToStringFormat("yyyyMMddHHmmss");
        }

        /// <summary>
        /// DataTime转换为yyyyMMddHHmmssfff
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringNumFull(this DateTime in_datetime)
        {
            return in_datetime.ClToStringFormat("yyyyMMddHHmmssfff");
        }

        /// <summary>
        /// DataTime转换为yyyy_MM_dd_HH_mm_ss
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringLine(this DateTime in_datetime)
        {
            return in_datetime.ClToStringFormat("yyyy_MM_dd_HH_mm_ss");
        }

        /// <summary>
        /// DataTime转换为yyyy_MM_dd_HH_mm_ss_fff
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringLineFull(this DateTime in_datetime)
        {
            return in_datetime.ClToStringFormat("yyyy_MM_dd_HH_mm_ss_fff");
        }

        /// <summary>
        /// DataTime转换为备份目录时格式
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringBackupFormat(this DateTime in_datetime)
        {
            return in_datetime.ToString("yyyy") + @"\" + in_datetime.ToString("yyyy_MM") + @"\" + in_datetime.ToString("yyyy_MM_dd");
        }

        /// <summary>
        /// DataTime转换为备份目录时带时间格式
        /// </summary>
        /// <param name="in_datetime"></param>
        /// <returns></returns>
        public static string ClToStringBackupFormatTime(this DateTime in_datetime)
        {
            return in_datetime.ToString("yyyy") + @"\" + in_datetime.ToString("yyyy_MM") + @"\" + in_datetime.ToString("yyyy_MM_dd") + @"\" + in_datetime.ToString("yyyy_MM_dd_HH_mm_ss");
        }




        /// <summary>
        /// 字符串转为日期时间型
        /// </summary>
        /// <param name="in_str"></param>
        /// <returns></returns>
        public static DateTime ClToDateTimeNum(this string in_str)
        {
            DateTime txtime;
            switch (in_str.Length)
            {
                case 8:
                    if (DateTime.TryParseExact(in_str, "yyyyMMdd", new CultureInfo("zh-CN"), DateTimeStyles.None, out txtime) == false)
                    {
                        return txtime;
                    }
                    break;
                case 14:
                    if (DateTime.TryParseExact(in_str, "yyyyMMddHHmmss", new CultureInfo("zh-CN"), DateTimeStyles.None, out txtime) == false)
                    {
                        return txtime;
                    }
                    break;
                case 17:
                    if (DateTime.TryParseExact(in_str, "yyyyMMddHHmmssfff", new CultureInfo("zh-CN"), DateTimeStyles.None, out txtime) == false)
                    {
                        return txtime;
                    }
                    break;
                default:
                    txtime = new DateTime(1900, 1, 1);
                    break;
            }
            return txtime;
        }

        /// <summary>
        /// 字符串转为日期时间型
        /// </summary>
        /// <param name="in_str"></param>
        /// <returns></returns>
        public static DateTime ClToDateTime(this string in_str, string in_format)
        {
            DateTime txtime;
            if (DateTime.TryParseExact(in_str, in_format, new CultureInfo("zh-CN"), DateTimeStyles.None, out txtime) == false)
            {
                return txtime;
            }

            return txtime;
        }

        public static DateTime ClToDateTime(this string in_str)
        {
            DateTime txtime;
            switch (in_str.Length)
            {
                case 10:
                    if (DateTime.TryParseExact(in_str, "yyyy-MM-dd", new CultureInfo("zh-CN"), DateTimeStyles.None, out txtime) == false)
                    {
                        return txtime;
                    }
                    break;
                case 19:
                    if (DateTime.TryParseExact(in_str, "yyyy-MM-dd HH:mm:ss", new CultureInfo("zh-CN"), DateTimeStyles.None, out txtime) == false)
                    {
                        return txtime;
                    }
                    break;
                case 23:
                    if (DateTime.TryParseExact(in_str, "yyyy-MM-dd HH:mm:ss.fff", new CultureInfo("zh-CN"), DateTimeStyles.None, out txtime) == false)
                    {
                        return txtime;
                    }
                    break;
                default:
                    txtime = new DateTime(1900, 1, 1);
                    break;
            }
            return txtime;
        }

        #endregion


        #region String


        public static byte[] HexStringToBytes(string hex)
        {
            if (hex.Length == 0)
            {
                return new byte[] { 0 };
            }

            if (hex.Length % 2 == 1)
            {
                hex = "0" + hex;
            }

            byte[] result = new byte[hex.Length / 2];

            for (int i = 0; i < hex.Length / 2; i++)
            {
                result[i] = byte.Parse(hex.Substring(2 * i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            }

            return result;
        }

        public static string BytesToHexString(byte[] input)
        {
            StringBuilder hexString = new StringBuilder(64);

            for (int i = 0; i < input.Length; i++)
            {
                hexString.Append(String.Format("{0:X2}", input[i]));
            }
            return hexString.ToString();
        }

        public static string BytesToDecString(byte[] input)
        {
            StringBuilder decString = new StringBuilder(64);

            for (int i = 0; i < input.Length; i++)
            {
                decString.Append(String.Format(i == 0 ? "{0:D3}" : "-{0:D3}", input[i]));
            }
            return decString.ToString();
        }

        // Bytes are string
        public static string ASCIIBytesToString(byte[] input)
        {
            System.Text.ASCIIEncoding enc = new ASCIIEncoding();
            return enc.GetString(input);
        }
        public static string UTF16BytesToString(byte[] input)
        {
            System.Text.UnicodeEncoding enc = new UnicodeEncoding();
            return enc.GetString(input);
        }
        public static string UTF8BytesToString(byte[] input)
        {
            System.Text.UTF8Encoding enc = new UTF8Encoding();
            return enc.GetString(input);
        }

        public static string GB2312BytesToString(byte[] input)
        {
            Encoding GB2312 = Encoding.GetEncoding("GB2312");
            return GB2312.GetString(input);
        }

        public static byte[] GB2312StringToBytes(string input)
        {
            Encoding GB2312 = Encoding.GetEncoding("GB2312");
            return GB2312.GetBytes(input);
        }
        // Base64
        public static string ToBase64(byte[] input)
        {
            return Convert.ToBase64String(input);
        }

        public static byte[] FromBase64(string base64)
        {
            return Convert.FromBase64String(base64);
        }

        public static string UnicodeToGB2312(string in_str)
        {
            //"country":"\u4e2d\u56fd"-->"country":"中国"
            //正则表达式匹配后，根据自己的值作替换
            Func<Match, string> MatchDoThing = m =>
            {
                byte[] bts = new byte[2];
                bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
                bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
                return Encoding.Unicode.GetString(bts);
            };

            string rgx = @"\\u([\w]{2})([\w]{2})";
            string result = Regex.Replace(in_str, rgx, new MatchEvaluator(MatchDoThing), RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return result;
        }

        public static string GB2312ToUnicode(string in_str)
        {
            //正则表达式匹配后，根据自己的值作替换
            Func<Match, string> MatchDoThing = m =>
            {
                byte[] bts = Encoding.Unicode.GetBytes(m.Groups[0].Value);
                string r = "";
                for (int i = 0; i < bts.Length; i += 2) r += "\\u" + bts[i + 1].ToString("x").PadLeft(2, '0') + bts[i].ToString("x").PadLeft(2, '0');
                return r;
            };

            string rgx = @"[\u4e00-\u9fa5]";
            string result = Regex.Replace(in_str, rgx, new MatchEvaluator(MatchDoThing), RegexOptions.IgnoreCase);
            return result;
        }

        //----------------------------------------------------------------------------------------------------------------------

        public static string[] SplitString(string in_str, string in_split, System.Text.RegularExpressions.RegexOptions regexOptions)
        {
            #region MD5String delegate
            Func<string, string> MD5String = delegate(string in_str1)
            {
                MD5 md5 = new MD5CryptoServiceProvider();

                byte[] buf = System.Text.Encoding.UTF8.GetBytes(in_str1);

                byte[] result = md5.ComputeHash(buf);

                string ret = "";
                for (int i = 0; i < result.Length; i++)
                {
                    ret += result[i].ToString("x").PadLeft(2, '0');
                }

                return ret.ToUpper();
            };
            #endregion

            string splitStr = MD5String(in_split);
            string str = in_str.Replace(in_split, splitStr);
            string[] arr = System.Text.RegularExpressions.Regex.Split(str, splitStr, regexOptions);

            return arr;
        }

        public static string[] SplitString(string in_str, string in_split)
        {
            return SplitString(in_str, in_split, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        }









        public static byte[] ConvertStringToBytes(string in_str)
        {
            try
            {
                return System.Text.Encoding.UTF8.GetBytes(in_str);
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }

        public static string ConvertBytesToString(byte[] in_bytes)
        {
            try
            {
                string str = System.Text.Encoding.UTF8.GetString(in_bytes);
                str = str.Substring(0, str.IndexOf('\0') >= 0 ? str.IndexOf('\0') : str.Length);//去掉无用字符                 
                return str;
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }

        public static byte[] ConvertStringToBytes64(string in_str)
        {
            try
            {
                return Convert.FromBase64String(in_str);
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }

        public static string ConvertBytesToString64(byte[] in_bytes)
        {
            try
            {
                return Convert.ToBase64String(in_bytes);
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }













        #region 字符串处理

        public static string MakePadl(string in_str, int in_len, char in_1_char)
        {
            try
            {
                if (in_len < 0) return in_str;
                return in_str.PadLeft(in_len, in_1_char);
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }

        }

        public static string MakePadl(string in_str, int in_len, char in_1_char, Encoding in_Encoding)
        {
            try
            {
                int l = in_Encoding.GetByteCount(in_str);

                string str = in_str;
                for (int i = 0; i < in_len - l; i++)
                {
                    str = in_1_char + str;
                }

                return str;
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }


        public static string MakePadr(string in_str, int in_len, char in_1_char)
        {
            try
            {
                if (in_len < 0) return in_str;
                return in_str.PadRight(in_len, in_1_char);
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }

        public static string MakePadr(string in_str, int in_len, char in_1_char, Encoding in_Encoding)
        {
            try
            {
                int l = in_Encoding.GetByteCount(in_str);

                string str = in_str;
                for (int i = l; i < in_len; i++)
                {
                    str += in_1_char;
                }

                return str;
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }


        public static string MakePadc(string in_str, int in_len, char in_1_char)
        {
            try
            {
                if (in_len < 0) return in_str;
                int i = (in_len - in_str.Length) / 2;
                string out_str = MakePadl("", i, in_1_char) + in_str;
                out_str = MakePadr(out_str, in_len, in_1_char);
                return out_str;
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }

        public static string MakePadc(string in_str, int in_len, char in_1_char, Encoding in_Encoding)
        {
            try
            {
                int i = (in_len - in_str.Length) / 2;
                string out_str = MakePadl("", i, in_1_char, in_Encoding) + in_str;
                out_str = MakePadr(out_str, in_len, in_1_char, in_Encoding);
                return out_str;
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }


        public static string Sleft(string in_str, int in_int)
        {
            try
            {
                if (in_int < 0) return in_str;
                return in_str.Substring(0, in_int);
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }

        public static string Sright(string in_str, int in_int)
        {
            try
            {
                if (in_int < 0) return in_str;
                if (in_int >= in_str.Length) return in_str;
                return in_str.Substring(in_str.Length - in_int, in_int);
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }



        public static bool StringHaveLower(string in_str)
        {
            try
            {
                for (int i = 0; i < in_str.Length; i++)
                {
                    string str = in_str.Substring(i, 1);
                    char chr = Convert.ToChar(str);
                    int asc = Convert.ToInt32(chr);
                    if ((asc >= 97) && (asc <= 122))
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }

        public static bool StringHaveQuota(string in_str)
        {
            try
            {
                for (int i = 0; i < in_str.Length; i++)
                {
                    string str = in_str.Substring(i, 1);
                    if (str == "'")
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }

        public static string RemoveQuota(string in_str)
        {
            try
            {
                return in_str.Replace("'", "");
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }

        public static string FormatQuota(string in_str)
        {
            try
            {
                return in_str.Replace("'", "''");
            }
            catch (Exception err)
            {
                FileClass.Log(err);
                throw;
            }
        }

        #endregion




        #region GetExcelCol

        public static string GetExcelCol(int in_int)
        {
            if (in_int < 1) return "";

            if (in_int % 26 == 0)
            {
                return GetExcelCol(Convert.ToInt32(in_int / 26) - 1) + Convert.ToChar(64 + 26);
            }
            else
            {
                return GetExcelCol(Convert.ToInt32(in_int / 26)) + Convert.ToChar(64 + in_int % 26);
            }
        }

        #endregion

        #endregion

        #region int

        public static int GetRandom(int minValue, int maxValue)
        {
            //使用Guid.NewGuid().GetHashCode()作为种子，可以确保Random在极短时间产生的随机数尽可能做到不重复    
            Random rand = new Random(Guid.NewGuid().GetHashCode());
            return rand.Next(minValue, maxValue + 1);
        }

        /// <summary>
        /// 返回一组唯一不重复的随机数    
        /// </summary>
        /// <param name="minValue">最小值</param>
        /// <param name="maxValue">最大值</param>
        /// <returns></returns>
        public static List<int> GetRandomList(int minValue, int maxValue)
        {
            List<int> Numbers = new List<int>();
            //使用Guid.NewGuid().GetHashCode()作为种子，可以确保Random在极短时间产生的随机数尽可能做到不重复    
            Random rand = new Random(Guid.NewGuid().GetHashCode());
            int item;
            for (int i = minValue; i <= maxValue; i++)
            {
                item = rand.Next(minValue, maxValue + 1);
                while (Numbers.IndexOf(item) != -1)
                {
                    item = rand.Next(minValue, maxValue + 1);
                }
                Numbers.Add(item);
            }
            return Numbers;
        }

        #endregion
    }











}


